﻿using Microsoft.EntityFrameworkCore;
using RbacPermissionSystem.Domain;
using System.Linq;
using System.Linq.Expressions;

namespace RbacPermissionSystem.Repositories
{
    public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
    {
        private readonly RbacDbContext db;

        public Repository(RbacDbContext db)
        {
            this.db = db;
        }

        #region 添加
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<TEntity> InsertAsync(TEntity entity)
        {
            db.Set<TEntity>().Add(entity);
            await db.SaveChangesAsync();
            return entity;
        }

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> InsertAsync(List<TEntity> entities)
        {
            await db.Set<TEntity>().AddRangeAsync(entities);
            return await db.SaveChangesAsync();
        }
        #endregion

        #region 更新
        /// <summary>
        /// 根据实体更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<TEntity> UpdateAsync(int PrimaryKey, TEntity entity)
        {
            var currEntity = await db.Set<TEntity>().FindAsync(PrimaryKey);
            db.Set<TEntity>().Update(entity);
            await db.SaveChangesAsync();
            return entity;
        }

        /// <summary>
        /// 根据实体更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<TEntity> UpdateAsync(TEntity entity)
        {
            db.Set<TEntity>().Update(entity);
            await db.SaveChangesAsync();
            return entity;
        }
        #endregion

        #region 删除
        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="PrimaryKey"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(int PrimaryKey)
        {
            var entity = await db.Set<TEntity>().FindAsync(PrimaryKey);
            db.Set<TEntity>().Remove(entity);
            return await db.SaveChangesAsync();
        }

        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="PrimaryKey"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(List<int> PrimaryKeys)
        {
            foreach (var item in PrimaryKeys)
            {
                var entity = await db.Set<TEntity>().FindAsync(PrimaryKeys);
                db.Set<TEntity>().Remove(entity);
            }
            return await db.SaveChangesAsync();
        }

        /// <summary>
        /// 按条件删除
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate)
        {
            var entities = db.Set<TEntity>().Where(predicate);
            foreach (var item in entities)
            {
                db.Set<TEntity>().Remove(item);
            }
            return await db.SaveChangesAsync();
        }
        #endregion

        #region 查询
        /// <summary>
        /// 按主键查
        /// </summary>
        /// <param name="PrimaryKey"></param>
        /// <returns></returns>
        public async Task<TEntity> FindAsync(int PrimaryKey)
        {
            return await db.Set<TEntity>().FindAsync(PrimaryKey);
        }

        /// <summary>
        /// 按条件查单条
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public async Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return await db.Set<TEntity>().FirstOrDefaultAsync(predicate);
        }

        /// <summary>
        /// 查全部
        /// </summary>
        /// <returns></returns>
        public async Task<List<TEntity>> QueryAsync()
        {
            return await db.Set<TEntity>().ToListAsync();
        }

        /// <summary>
        /// 查全部
        /// </summary>
        /// <returns></returns>
        public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> predicate = null)
        {
            return await db.Set<TEntity>().Where(predicate).ToListAsync();
        }

        /// <summary>
        /// 返回IQueryable
        /// </summary>
        /// <returns></returns>
        public IQueryable<TEntity> Queryable()
        {
            return db.Set<TEntity>();
        }

        /// <summary>
        /// 判断是否存在该数据
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public async Task<bool> IsExistsAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return await db.Set<TEntity>().AnyAsync(predicate);
        }
        #endregion
    }
}